package com.vpx.server.controller.system;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.vpx.server.bean.order.base.BaseDelOrder;
import com.vpx.server.bean.order.base.BaseIdStrOrder;
import com.vpx.server.bean.order.system.ProjectConfigOrder;
import com.vpx.server.bean.order.system.ProjectConfigPageOrder;
import com.vpx.server.bean.result.system.ProjectConfigResult;
import com.vpx.server.framework.authority.AuthPoicy;
import com.vpx.server.framework.authority.RequestAuth;
import com.vpx.server.framework.base.BasePageResult;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.service.system.ProjectConfigService;
import com.vpx.server.service.system.SqlExtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 工程管理
 */
@RestController
@RequestMapping(value = "/api/projectConfig")
@Api(tags = "工程基础配置管理")
public class ProjectConfigController {

    @Autowired
    private ProjectConfigService projectConfigService;
    @Autowired
    private SqlExtService sqlExtService;
    @PostMapping("/insertPorjectConfig")
    @ApiOperation("新增工程基础配置")
    //@RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg insert(@RequestBody ProjectConfigOrder order) {
        return projectConfigService.saveProjectConfig(order);
    }



    @DeleteMapping("/deleteProjectConfig")
    @ApiOperation("删除工程基础配置")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg deleteProjectConfig(@Validated @RequestBody BaseDelOrder order) {
        return projectConfigService.deleteProjectConfig(order);
    }

    @PostMapping("/updateProjectConfig")
    @ApiOperation("修改工程基础配置")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg updateById(@RequestBody ProjectConfigOrder order) {
        return projectConfigService.updateProjectConfigById(order);
    }

    @GetMapping("/selectProjectConfigById")
    @ApiOperation("查询工程基础配置")
    @RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg<ProjectConfigResult> selectById(BaseIdStrOrder order) {
        return projectConfigService.selectProjectById(order);
    }

    @GetMapping("/pageQueryProjectConfig")
    @ApiOperation("分页查询工程基础配置")
    //@RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg<BasePageResult<ProjectConfigResult>> pageQueryProjectConfig(ProjectConfigPageOrder order) {

        return projectConfigService.pageQueryProjectConfig(order);
    }

    @GetMapping("/pageQueryOmnibusConfig")
    @ApiOperation("分页查询总线配置")
    //@RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg<BasePageResult<ProjectConfigResult>> pageQueryOmnibusConfig(ProjectConfigPageOrder order) {

        return projectConfigService.pageQueryOmnibusConfig(order);
    }

    @GetMapping("/queryOmnibusConfig")
    @ApiOperation("分页查询总线配置")
    //@RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg<List<JSONObject>> queryOmnibusConfig(ProjectConfigPageOrder order) {
        String projectId = order.getProjectId();
        String configType = order.getConfigType();
        String sql = "select * from c_project_config where config_type = '"+configType+"'";
//        if(StringUtils.isNotBlank(projectId)) {
//            sql = sql + " and project_id = '"+projectId+"'";
//        }
        return ResultMsg.success(sqlExtService.findListObject(sql,JSONObject.class));
    }
    @PostMapping("/batchSaveEditPorjectConfig")
    @ApiOperation("批量新增修改工程基础配置")
    //@RequestAuth(poicy = AuthPoicy.AUTH)
    public ResultMsg batchSaveEdit(@RequestBody JSONObject param) {
        String configType = param.getByPath("configType",String.class);
        JSONArray ja = param.getByPath("orders",JSONArray.class);
        List<ProjectConfigOrder> orders = new ArrayList<>();
        if(ja != null && ja.size() > 0) {
            for(int i = 0;i < ja.size(); i++) {
                orders.add(JSONUtil.toBean(ja.getJSONObject(i),ProjectConfigOrder.class));
            }
        }
        return projectConfigService.batchSaveEditProjectConfig(orders, configType);
    }
}
